import dateFormat from "vux/src/tools/date/format.js";

import dataTimeFilter from "../../common/dataTimeFilter/dataTimeFilter";
import { addClass, removeClass } from "@/assets/js/dom.js";
import template from "./DealRecordTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  components: {
    dataTimeFilter,
  },
  data() {
    return {
      showList: [],
      nowId: "",
      recordList: [],

      dealTypeIdList: [],
      dealType: "",
      dealTypeId: "",

      status: "",
      statusId: "",
      type: "",
      statusList: [
        { id: 0, name: "全部状态" },
        { id: 1, name: "处理中" },
        { id: 2, name: "已完成" },
      ],
      pikerShow: false,
      pickerTitle: "彩种类型",
      datePicker: false,

      beginTime: dateFormat(new Date(), "YYYY-MM-DD"),
      endTime: dateFormat(new Date(), "YYYY-MM-DD"),
      page: 1,

      upOne: false,
      upTwo: false,

      single_money: [],
      total_money: null,
    };
  },
  created() {
    this.getBills();
  },
  methods: {
    activeCopy(e) {
      addClass(e.target, "activeScroll");
      document.addEventListener("touchmove", () => {
        removeClass(e.target, "activeScroll");
      });
      document.addEventListener("touchend", () => {
        removeClass(e.target, "activeScroll");
      });
    },
    showDetail(show, type, order_no, name) {
      if (show != 0) {
        this.$router.push(
          "/recordDetail?type=" +
            type +
            "&order_no=" +
            order_no +
            "&name=" +
            name
        );
      }
    },
    pullingDown() {
      this.page = 1;
      this.getBills(1);
    },
    pullingUp() {
      if (this.pullUpLoadFlag) {
        this.$refs.scroll.forceUpdate(false);
        return;
      }
      this.page += 1;
      this.getBills(1);
    },
    resetAll(reset) {
      let vm = this;
      vm.dealType = vm.dealTypeIdList[0];
      vm.status = vm.statusList[0];
      vm.dealTypeId = vm.dealTypeIdList[0].id;
      vm.statusId = vm.statusList[0].id;
      vm.beginTime = dateFormat(new Date(), "YYYY-MM-DD");
      vm.endTime = dateFormat(new Date(), "YYYY-MM-DD");
      vm.page = 1;
      if (reset) {
        return;
      }
      vm.getBills();
    },
    showPicker(type, id) {
      this.type = type;
      if (type == 0) {
        this.showList = this.dealTypeIdList;
        this.pickerTitle = "交易类型";
        this.upOne = true;
      } else if (type == 1) {
        this.showList = this.statusList;
        this.pickerTitle = "交易状态";
        this.upTwo = true;
      }
      this.pikerShow = true;
      this.nowId = id;
    },
    getBills(flag) {
      let vm = this;
      vm.pullUpLoadFlag = false;
      if (flag != 1) {
        vm.$vux.loading.show();
      }
      let params = {};
      params.token = localStorage.getItem("token");
      params.start_time = vm.beginTime;
      params.end_time = vm.endTime;
      params.status = vm.statusId;
      params.type = vm.dealTypeId;
      params.page = vm.page;
      if (vm.$route.query.user_id) {
        params.user_id = vm.$route.query.user_id;
        params.source = 2;
      }
      vm.$http
        .post(vm.urlRequest + "?m=api&c=account&a=getBills", params)
        .then((res) => {
          if (res.status == 0) {
            vm.total_money = res.total_money;
            vm.single_money = res.single_money || [];
            if (vm.dealTypeIdList.length == 0) {
              vm.dealTypeIdList = res.trantype;
              vm.dealTypeIdList = vm.dealTypeIdList.map((item, index) => {
                if (index == 0) {
                  return {
                    name: "全部类型",
                    id: item.id,
                  };
                } else {
                  return item;
                }
              });
              vm.resetAll("reset");
            }
            if (vm.page == 1) {
              vm.recordList = res.list;
            } else {
              vm.recordList = vm.recordList.concat(res.list);
            }
            if (res.list.length < 20) {
              vm.pullUpLoadFlag = true;
            }
          }
          vm.$vux.loading.hide();
        })
        .catch((err) => {
          vm.$vux.loading.hide();
          console.log(err);
        });
    },
    pickHide(data) {
      this.datePicker = data;
    },
    getTime(data) {
      let begin = new Date(data[0]);
      let end = new Date(data[1]);
      this.beginTime = data[0];
      this.endTime = data[1];
      this.datePicker = false;
      this.page = 1;
      this.getBills();
    },
    picking(e, id) {
      if (this.type == 0) {
        this.dealTypeId = id;
        this.dealType = this.dealTypeIdList.find((item) => {
          return item.id == id;
        });
      } else if (this.type == 1) {
        this.statusId = id;
        this.status = this.statusList.find((item) => {
          return item.id == id;
        });
      }
      this.nowId = id;
      this.page = 1;
      this.getBills();
      let time = setTimeout(() => {
        this.pikerShow = false;
      }, 200);
    },
  },
  watch: {
    pikerShow() {
      if (this.pikerShow == false) {
        this.upOne = false;
        this.upTwo = false;
      }
    },
    $route(to, from) {
      if (from.name != "RecordDetail" && to.name == "DealRecord") {
        this.recordList = [];
        this.resetAll(0);
      }
    },
  },
  activated() {},
  deactivated() {},
};

// WEBPACK FOOTER //
// ./src/components/wallet/record/DealRecord.vue
